<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * index.ctrl
 * 全部商品列表控制器
 */
defined('IN_IA') or exit('Access Denied');
wl_load()->model('adv');
wl_load()->model('goods');
wl_load()->model('merchant');
$_SESSION['type']=NULL;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';

//全民兼职  开始
$tid=8167;
//权限控制
wl_load()->model('functions');
$checkfunction=checkfunc($tid);
$miaoxianfunction=checkfunc(8169);
$banquanfunction=checkfunc(8170);
wl_load()->model('member');
$member=member_get_by_params("from_user='".$_W['openid']."' and uniacid='".$_W['uniacid']."'");
$acct = pdo_fetch("select tpl,homeimg from " . tablename('account_wechats') . " where  uniacid = '{$_W['uniacid']}'");
$checkhomeimg=checkfunc(8169);
if($checkhomeimg['status']==0){
	$acct['homeimg']='';
}
$tourl=app_url('goods/list');
if($checkfunction['status']&&$member['enable']==1)
{
	$tourl=app_url('goods/list')."&mid=".$member['id'];
}
if($acct['tpl']!='8146'){
	$tplfunction=checkfunc($acct['tpl']);
	if($tplfunction['status']==0)
	{
		pdo_update('account_wechats',array('tpl'=>8146),array('uniacid' =>$_W['uniacid']));
		$acct = pdo_fetch("select tpl,homeimg from " . tablename('account_wechats') . " where  uniacid = '{$_W['uniacid']}'");
	}
}
$tpl='goods/goods_list1';//默认拼团
$tpl1='goods/goods_list2';//默认商团
if($acct['tpl']=='8146')
{
	$tpl1='index';
	$tpl='index';
}
if($acct['tpl']=='8147')
{
	$tpl1='goods/goods_list1';
	$tpl='goods/goods_list2';
}
if($acct['tpl']=='8172')
{
	$tpl1='goods/goods_list1';
	$tpl='goods/goods_list3';
}
if($acct['tpl']=='8174')
{
	$tpl1='goods/goods_list5';
	$tpl='goods/goods_list5';
}
if($acct['tpl']=='8175')
{
	$tpl1='goods/goods_list5';
	$tpl='goods/goods_list4';

}
if($acct['tpl']=='8190')
{
	$tpl1='goods/goods_list6';
	$tpl='goods/goods_list6';

}
if($acct['tpl']=='8193')
{
	$tpl1='goods/goods_list7';
	$tpl='goods/goods_list7';

}
if($acct['tpl']=='8196')
{
	$tpl1='goods/goods_list8';//拼团
	$tpl='goods/goods_list8';

}
if($checkfunction['status']&&intval($_GPC['sharenum'])!=0&&$member['parentid']==-1)
{
	
	$data=array('parentid'=>$_GPC['sharenum']);
	$params=array('from_user'=>$_W['openid']);
	member_update_by_params($data,$params);	
}
$member=member_get_by_params("from_user='".$_W['openid']."'");
if(!$checkfunction['status']||$member['parentid']==-1)
{
	$data=array('parentid'=>99);
	$params=array('from_user'=>$_W['openid']);
	member_update_by_params($data,$params);
}

//全民兼职 结束
if($op =='display'){
	puvindex($_W['openid']);
	$pagetitle = !empty($config['tginfo']['sname']) ? $config['tginfo']['sname'] : '乐享拼购';

	$cid = intval($_GPC['gid']);

	$advs = pdo_fetchall("select * from " . tablename('tg_adv') . " where enabled = 1 and uniacid = '{$_W['uniacid']}' order by displayorder DESC");
	foreach ($advs as &$adv) {
		if (substr($adv['link'], 0, 5) != 'http:') {
			$adv['link'] = "http://" . $adv['link'];
		}
	}
	unset($adv);
	
	$category = pdo_fetchall("SELECT * FROM " . tablename('tg_category') . " WHERE uniacid = '{$_W['uniacid']}' and enabled=1 and parentid=0 ORDER BY parentid ASC, displayorder DESC");
	if($cid>0)
	{
		$cat = pdo_fetch("SELECT * FROM " . tablename('tg_category') . " WHERE uniacid = '{$_W['uniacid']}' and id='{$cid}'");
		if($cat['selltype']==0)
		{
			include wl_template($tpl);
		}else{
			include wl_template($tpl1);
		}
	}else{
		include wl_template($tpl);
	}
	
}
if($op =='shopajax'){
	$page = $_GPC['page'];
	$pagesize = $_GPC['pagesize'];
	$cid = intval($_GPC['cid']);
	$tuan=0;
	if($cid>0)
	{
		$categ =pdo_fetch("SELECT * FROM " . tablename('tg_category') . " WHERE id = '{$cid}' ");
		$tuan=intval($categ['selltype']);
	}
	$keyword =!empty($_GPC['keyword'])? $_GPC['keyword'] : '';

	$data = goods_get_list(array('usepage'=>1,'ishows'=>'1,3','page'=>$page,'pagesize'=>$pagesize,'lin'=>0,'tuan'=>$tuan,'cid' => $cid,'gname' => $keyword,'orderby' => 'ORDER BY displayorder DESC'));
	foreach ($data['list'] as $key => $value) {
		$params = pdo_fetchall("SELECT * FROM" . tablename('tg_goods_param') .  "WHERE goodsid = '{$value['id']}' limit 0,3 ");
		$data['list'][$key]['params'] = $params;
		//规格及规格项
	$options = pdo_fetchall("select id,title,thumb,marketprice,productprice,costprice,specs from " . tablename('tg_goods_option') . " where goodsid=:id order by id asc", array(':id' => $value['id']));
	$data['list'][$key]['options']=count($options);
	$old_data=pdo_fetch("select * from ".tablename('tg_goods_openid').' where uniacid=:uniacid and openid=:openid and g_id=:g_id',array(':uniacid'=>$_W['uniacid'],':openid'=>$_W['openid'],':g_id'=>$value['id']));
							
	$data['list'][$key]['history_limit'] =intval($old_data['num']);
	
	
	
	}
	$goodses = $data;
	die(json_encode($goodses));
}
if($op =='ajax'){
	$page = $_GPC['page'];
	$pagesize = $_GPC['pagesize'];
	$cid = intval($_GPC['cid']);
	$wechats = pdo_fetch("select tpl from " . tablename('account_wechats') . " where uniacid = '{$_W['uniacid']}'");
	$func = pdo_fetch("select * from " . tablename('tg_function') . " where id = '{$wechats['tpl']}'");

	//邻购
	$lin=checkfunc(8161);

	$data = goods_get_list(array('usepage'=>1,'ishows'=>'1,3','page'=>$page,'pagesize'=>$pagesize,'lin'=>$lin['status'],'tuan'=>1,'cid' => $cid,'orderby' => 'ORDER BY displayorder DESC'));
	foreach ($data['list'] as $key => $value) {
		$params = pdo_fetchall("SELECT * FROM" . tablename('tg_goods_param') .  "WHERE goodsid = '{$value['id']}' limit 0,3 ");
		$data['list'][$key]['params'] = $params;
		//规格及规格项
	$options = pdo_fetchall("select id,title,thumb,marketprice,productprice,costprice,specs,weight from " . tablename('tg_goods_option') . " where goodsid=:id order by id asc", array(':id' => $value['id']));
	$data['list'][$key]['options']=count($options);
	$old_data=pdo_fetch("select * from ".tablename('tg_goods_openid').' where uniacid=:uniacid and openid=:openid and g_id=:g_id',array(':uniacid'=>$_W['uniacid'],':openid'=>$_W['openid'],':g_id'=>$value['id']));

	$data['list'][$key]['history_limit'] =intval($old_data['num']);
	}
	$goodses = $data;
	die(json_encode($goodses));
}
if($op =='weight'){
	
	$strname = $_GPC['strname'];

	$option = pdo_fetch("select * from " . tablename('tg_goods_option') . " where title=:id ", array(':id' => $strname));
	
	die(json_encode($option));
	
}
if($op =='clear')
{
	if(pdo_delete('tg_collect', array('openid' =>$_W['openid'],'orderno'=>0)))
	{
		echo 1;
		exit;
	}else{
		echo 0;
		exit;
	}
}
if($op =='specsajax')
{
	$id=$_GPC['id'];
	$allspecs = pdo_fetchall("select * from " . tablename('tg_spec') . " where goodsid=:id order by displayorder asc", array(':id' => $id));
	foreach ($allspecs as &$s) {
		$s['items'] = pdo_fetchall("select * from " . tablename('tg_spec_item') . " where  `show`=1 and specid=:specid order by displayorder asc", array(":specid" => $s['id']));
	
	}
	
	unset($s);
	$options = pdo_fetchall("select id,title,thumb,marketprice,productprice,costprice,specs,stock,weight from " . tablename('tg_goods_option') . " where goodsid=:id order by id asc", array(':id' => $id));
	$specs = array();
	if (count($options) > 0) {
		$specitemids = explode("_", $options[0]['specs'] );
		foreach($specitemids as $itemid){
			foreach($allspecs as $ss){
				$items = $ss['items'];
				foreach($items as $it){
					if($it['id']==$itemid){
						$specs[] = $ss;
						break;
					}
				}
			}
		}
	}
	foreach ($options as $key => $value) {
		$th=pdo_fetch('select thumb from '.tablename('tg_spec_item').' where id=:id',array(':id'=>$value['specs']));
		$options[$key]['thumb']=tomedia($th['thumb']);
		$options[$key]['specs']=$specs;
	}
	die(json_encode($options));
}

if($op =='add'){
	$id=$_GPC['id'];
	$str=$_GPC['str'];//规格
	$weight=$_GPC['weight'];//规格
	$kunum1 = pdo_fetch("SELECT productprice,stock FROM " . tablename('tg_goods_option') . " WHERE   goodsid='{$id}' and title='{$str}'  ");
	$price=$kunum1['productprice'];
	$gnum=$kunum1['stock'];
			if (empty($id)) {
				echo 0;
				exit;
			}else{
				$sql = 'SELECT oprice,supprices,storeid,credit,commissiontype,commission FROM '.tablename('tg_goods').' WHERE id=:id and uniacid=:uniacid';
		$paramse = array(':id'=>$id, ':uniacid'=>$_W['uniacid']);
		$goods = pdo_fetch($sql, $paramse);
		if(empty($str))
	{
		$price=$goods['oprice'];
	}
				$data=array(
	            'openid' => $_W['openid'],
	            'uniacid'=>$_W['uniacid'],
				'num'=>1,
				'oprice'=>$price,
				'orderno'=>0,
				'applystatus'=>0,
				'optionid'=>0,
				'item'=>$_GPC['str'],
				'weight'=>$weight,
				'supprices'=>$goods['supprices'],
				'storeid'=>$goods['storeid'],
				'credit'=>$goods['credit'],
				'type'=>$goods['commissiontype'],
				'commission'=>$goods['commission'],
	            'sid'=>$id
	            );
				
				$tt = pdo_fetch("SELECT id,num FROM " . tablename('tg_collect') . " WHERE  uniacid = '{$_W['uniacid']}' and sid='{$id}'  and openid='{$_W['openid']}' and item='{$str}'  and orderno='0'");
				$kunum = pdo_fetch("SELECT gnum FROM " . tablename('tg_goods') . " WHERE  uniacid = '{$_W['uniacid']}' and id='{$id}'  ");
				if(intval($gnum)==0&&empty($str))
				{
					$gnum=$kunum['gnum'];
				}
				$num=$tt['num']+1;
				if($num>intval($gnum)){echo '-1';exit;}
				if(empty($tt)){
	            if (pdo_insert('tg_collect', $data)) {
	            	echo 1;
	            }else{
	            	    //
					
			echo 0;
						//
	            }
				}
				else
				{
					pdo_update('tg_collect',array('num'=>$num),array('id' =>$tt['id']));
					echo 1;
				}
			}
	exit();
}
if($op =='remove'){
	$id=$_GPC['id'];
	$str=$_GPC['str'];//规格
	if (empty($id)) {
				echo 0;
				exit;
			}else{
				//
				$tua = pdo_fetch("SELECT num,id FROM " . tablename('tg_collect') . " WHERE  sid = {$id} and uniacid = '{$_W['uniacid']}' AND orderno = '0' and item='{$str}'  and openid='{$_W['openid']}'");				
				$num=$tua['num']-1;
				if($num>0)
				{
				
				if (pdo_update('tg_collect',array('num'=>$num),array('id' =>$tua['id']))) {
					echo $num;
				}
				}
				else
				{					
					if (pdo_delete('tg_collect', array('id' =>$tua['id']))) {
					echo 0;
					}
				}
			//
				
				
			}
	exit();
}

if($op =='notice'){
	$id=$_GPC['id'];
	$type=$_GPC['type'];//规格
	$tua = pdo_fetch("SELECT id FROM " . tablename('tg_notice') . " WHERE  g_id = {$id} and uniacid = '{$_W['uniacid']}'  and openid='{$_W['openid']}' and type='{$type}'");				
	if(empty($tua))
	{
		pdo_insert('tg_notice',array('uniacid'=>$_W['uniacid'],'openid'=>$_W['openid'],'g_id'=>$id,'type'=>$type));
		echo 1;
		exit;
	}else{
		echo 1;
		exit;
	}		
				
}
if($op =='noticeremove'){
	$id=$_GPC['id'];
	$type=$_GPC['type'];//规格
	$tua = pdo_fetch("SELECT id FROM " . tablename('tg_notice') . " WHERE  g_id = {$id} and uniacid = '{$_W['uniacid']}'  and openid='{$_W['openid']}' and type='{$type}'");				
	if (pdo_delete('tg_notice', array('id' =>$tua['id']))) {
					echo 1;
					}	
				
}
if($op =='search'){
	$keyword = $_GPC['keyword'];
	//$goods = goods_get_list(array('gname'=>$keyword));
	include wl_template($tpl);
}
if($op =='merchant'){
	$id = $_GPC['id'];
	$merchant = merchant_get_by_params("id = {$id}");
	include wl_template('goods/merchant_goods');
}
if($op =='merchant_ajax'){
	$id = $_GPC['id'];//商家id
	$page = $_GPC['page'];
	$pagesize = $_GPC['pagesize'];
	$data = goods_get_list(array('usepage'=>1,'ishows'=>'1,3','page'=>$page,'pagesize'=>$pagesize,'merchantid' => $id,'orderby' => 'ORDER BY displayorder DESC'));
	foreach ($data['list'] as $key => $value) {
		$params = pdo_fetchall("SELECT * FROM" . tablename('tg_goods_param') .  "WHERE goodsid = '{$value['id']}' limit 0,3 ");
		$data['list'][$key]['params'] = $params;
	}
	$goodses = $data;
	die(json_encode($goodses));
}
exit();